/* Project specific Javascript goes here. */
function getCookie(name) {
    var cookieValue = null;
    if (document.cookie && document.cookie !== '') {
        var cookies = document.cookie.split(';');
        for (var i = 0; i < cookies.length; i++) {
            var cookie = jQuery.trim(cookies[i]);
            // Does this cookie string begin with the name we want?
            if (cookie.substring(0, name.length + 1) === (name + '=')) {
                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                break;
            }
        }
    }
    return cookieValue;
}
var csrftoken = getCookie('csrftoken') ? getCookie('csrftoken') : jQuery("[name=csrfmiddlewaretoken]").val();

function csrfSafeMethod(method) {
    // these HTTP methods do not require CSRF protection
    return (/^(GET|HEAD|OPTIONS|TRACE)$/.test(method));
}


$.ajaxSetup({
    beforeSend: function(xhr, settings) {
        if (!csrfSafeMethod(settings.type) && !this.crossDomain) {
            xhr.setRequestHeader("X-CSRFToken", csrftoken);
        }
    }
});

$(function () {
    $('a.changePwd.item').on('click', function (e) {
      // console.log(this)
      let $changePwdForm = $('#changePwdForm'),
          $changePwdModal = $('.changePwd.modal')
      $changePwdModal.modal({
        closable: false
      }).modal('show')
      $changePwdForm.form({
        fields: {
          old_password: ['empty',],
          new_password1: ['empty', 'minLength[8]'],
          new_password2: ['empty', 'minLength[8]'],
        },
      })
      $('.changePwd.submit.button').on('click', ()=>{
        $changePwdForm.form('validate form');
        let $self = $(this);
        if($changePwdForm.form('is valid')) {
          let data = $changePwdForm.form('get values')
          data.old_password = simpleCBCEncrypt(data.old_password)
          data.new_password1 = simpleCBCEncrypt(data.new_password1)
          data.new_password2 = simpleCBCEncrypt(data.new_password2)
          console.log(data)
          $.ajax({
            url: '/users/change_password/',
            type: 'POST',
            data: data,
            beforeSend: function () {
              $self.addClass('loading disabled');
            },
            error: function (xhr, status, error) {
              $changePwdForm.form('add errors', error)
              $changePwdForm.addClass(status);
            },
            complete: function (xhr, status) {
              $self.removeClass('loading disabled');
            }
          }).done(function (res) {
            if (!res.success) {
              if(res.errors) {
                $changePwdForm.form('add errors', res.errors)
                if(res.errors.hasOwnProperty('new_password1') || res.errors.hasOwnProperty('new_password2')){
                  $changePwdForm.form('add prompt', 'new_password1')
                  $changePwdForm.form('add prompt', 'new_password2')
                }
                if(res.errors.hasOwnProperty('old_password')){
                  $changePwdForm.form('add prompt', 'old_password')
                }
              }
            }
            else{
              toastr["success"](res.message);
              $changePwdModal.modal({
                onHidden: function (){
                  $changePwdForm.form('reset')
                }
              }).modal('toggle')
            }
          })
        }
      })
    })
})
